<template>
  <div class="rightBodyContent">
    <div class="rightBodyContent-in">
      <tips optionTemplate="studentParents"></tips>
      <!--      搜索组件-->
      <SearchModle dataKey="studentParents" @searchFn="searchFn"></SearchModle>
      <!--      end-->
      <div class="mb-10">
        <div class="toplineBox">
          <div class="toplineBox-left">
            <el-button size="small" @click="addStudent()" type="primary"><i class="el-icon-plus"></i>添加家长</el-button>
          </div>
          <p class="numberline toplineBox-right">总{{ page.total }}条</p>
        </div>
      </div>
      <!--本体列表，数据 -->
      <div class="table-body">
        <div class="table-body-box">
          <el-table
              ref="multipleTable"
              :data="arryList"
              :stripe="tableStyle.tableStripe"
              :border="tableStyle.tableBorder"
              tooltip-effect="dark"
              header-cell-class-name="table-text-c"
              style="width: 100%">
            <el-table-column
                prop="id"
                label="编号"
                width="90"
                align="center">
            </el-table-column>
            <el-table-column
                prop="name"
                label="家长姓名"
                align="center">
            </el-table-column>
            <el-table-column
                prop="create_time"
                label="学生姓名"
                align="center">
              <template slot-scope="scope">
                {{ scope.row.student ? scope.row.student.name : '未知' }}
              </template>
            </el-table-column>
            <el-table-column
                label="亲子关系"
                align="center">
              <template slot-scope="scope">
                <span v-if="scope.row.relation">{{ scope.row.relation }}</span>
                <span v-else>未匹配</span>
              </template>
            </el-table-column>
            <el-table-column
                prop="mobile_phone"
                label="电话"
                align="center">
              <template slot-scope="scope">
                <el-button size="mini" type="primary" @click="$verifyCheking('phone', scope.row.mobile_phone)" plain>
                  查看电话
                </el-button>
<!--                <span class="checklog-pass" @click="$verifyCheking('phone', scope.row.mobile_phone)">查看电话</span>-->
              </template>
            </el-table-column>
            <!--            <el-table-column-->
            <!--                prop="role"-->
            <!--                label="微信绑定"-->
            <!--                align="center">-->
            <!--              <template slot-scope="scope">-->
            <!--                <div class="wx-user-box" v-if="scope.row.openid">-->
            <!--                  <el-popover-->
            <!--                      placement="left"-->
            <!--                      width="200"-->
            <!--                      trigger="hover"-->
            <!--                  >-->
            <!--                    <span v-if="scope.row.openid" class="wx-user-box-line">-->
            <!--                      <img :src="scope.row.wx.pic" alt="">{{ scope.row.wx.user_name }}-->
            <!--                     </span>-->
            <!--                    <span slot="reference" class="btn-wx">已绑定</span>-->
            <!--                  </el-popover>-->
            <!--                  <span class="delet-wx" @click="deletWx(scope.$index)">(解除绑定)</span>-->
            <!--                </div>-->
            <!--              </template>-->
            <!--            </el-table-column>-->
            <el-table-column
                align="center"
                label="操作"
                width="270">
              <template slot-scope="scope">
                <el-button size="small" @click="addStudent(scope.row)">查看详情</el-button>
                <el-button size="small" @click="changePassWordFn(scope.row)">密码重置</el-button>
                <el-button type="danger" size="small" @click="detelFn(scope.row.id)" plain>删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <!--工具条 分页-->
      <el-col :span="24" class="toolbar">
        <el-pagination
            background
            layout="total, prev, pager, next"
            :total="page.total"
            :page-size="page.pagesize"
            @current-change="handleSizeChange">
        </el-pagination>
      </el-col>
    </div>
    <parentsPopup :visible.sync="parentsVisible" :parentsOption="parentsOption"></parentsPopup>
  </div>
</template>

<script>
import {pagination, tableoption} from '@/utils/util.js'
import parentsPopup from "./parentsPopup";
import {parentsDelete, resetPassWord} from "@/api/admin/parents";

export default {
  components: {
    parentsPopup
  },
  data() {
    return {
      handleSelection: [],
      arryList: [], // 页面数据列表
      // 设置table样式
      tableStyle: tableoption,
      // 设置table页面分页所需
      page: {
        pageindex: pagination.pageIndex,
        pagesize: pagination.pageSize,
        total: pagination.total
      },
      indexOption: '',
      classesListNew: [],
      sectionList: [],
      classesList: [],
      editForm: {
        mobile_phone: '',
        p: '',
        name: '', // 	整型 	必填 	1 	- 	名称
        class_id: '', //  	整型 	必填 	1 	- 	户籍所属镇（街）
        section_id: '' //  	整型 	必填 	1 	- 	户口
      },
      // 搜索组件
      searchForm: {},
      parentsVisible: false,
      parentsOption: ''
    }
  },
  created() {
    this.InitFn()
  },
  methods: {
    // 初始化
    InitFn() {
      this.searchForm.p = this.page.pageindex
      this.$fetch(this.api.admin_parents_index, this.searchForm).then((response) => {
        if (response.code === 0) {
          this.arryList = JSON.parse(JSON.stringify(response.list.data))
          this.page.total = response.list.total
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    // 分页
    handleSizeChange(val) {
      this.page.pageindex = val
      this.InitFn()
    },
    // 搜索组件
    searchFn(option) {
      this.page.pageindex = 1
      this.searchForm = option
      this.InitFn()
    },
    addStudent(row) {
      this.parentsOption = row;
      this.parentsVisible = true;
      //     this.parentsOption = row
      // let id = row || ''
      // this.$router.push('/studentParentsdetail?editId=' + id)
    },
    deletWx(index) {
      let n = this;
      this.$confirm('确认解除' + this.arryList[index].name + '微信绑定, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.closeBinding(index, n.arryList[index].id)
      }).catch(() => {
      })
    },
    closeBinding(index, id) {
      let n = this;
      this.$post(this.api.patents.closeWxBind, {id: id}).then((response) => {
        if (response.code === 0) {
          n.arryList[index].openid = ''
          this.$message({
            type: 'success',
            message: '解绑成功!'
          })
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    detelFn(id) {
      let self_ = this
      this.$confirm('家长账号永久删除，确认删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        parentsDelete({id: id}).then(res => {
          if (!res.code) {
            this.$message.success('删除成功！')
            self_.InitFn()
          }
        })
      }).catch(() => {

      })
    },
    // 重置密码
    changePassWordFn(row) {
      this.$confirm(`确认重置【${row.name}】家长家园共育账号密码，重置后密码为jz加电话后6位, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.changePass(row)
      }).catch(() => {
      })
    },
    changePass(row) {
      resetPassWord({id: row.id}).then(res => {
        if (!res.code) {
          this.$message.success('重置成功！')
        }
      })
    },
  },
  mounted() {
  },
  // 缓存机制 star
  beforeRouteEnter(to, from, next) {
    // 利用路由元信息中的 meta 字段设置变量，方便在各个位置获取。这就是为什么在 meta 中定义 isNeedRefresh。
    // 当从详情页返回时，将 isNeedRefresh 设为 false，表示不刷新数据
    if (to.meta.isNeedRefresh) {
      this.InitFn() // 详情页面需要刷新数据
    }
    next()
  },
  beforeRouteLeave(to, from, next) {
    // isNeedRefresh 进入页面判断是否需要刷新
    if (to.meta.isNeedRefresh) {
      from.meta.keepAlive = true
    } else {
      from.meta.keepAlive = false
    }
    next()
  }
  // 缓存机制 end
}
</script>

<style scoped>

.wx-user-box-line img {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  margin-right: 5px;
}

.wx-user-box-line {
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-wx {
  cursor: pointer;
  padding: 5px;
}

.btn-wx:hover {
  color: #1b8de5;
  text-decoration: underline;
}

.delet-wx {
  color: red;
  cursor: pointer;
}

.delet-wx:hover {
  text-decoration: underline;
}
</style>
